﻿using System;
using System.Net;
using SverigesRadio.SverigesRadioApp.Business;

namespace Infrastructure
{
    public class SverigesRadioServiceCaller<T> : ServiceCallerBase<T>, IServiceCaller<T>
    {
        public event EventHandler<ServiceCallerEventArgs<T>> OnServiceCallReceived;
        public int Delay { get; set; }

        public SverigesRadioServiceCaller()
        {
            
        }

        public SverigesRadioServiceCaller(int delay)
        {
            Delay = delay;

        }

        public override void CallAsync(Uri serviceUri)
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += DownloadStringCompleted;

            wc.DownloadStringAsync(serviceUri);

        }

        void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (OnServiceCallReceived != null)
            {
                System.Threading.Thread.Sleep(Delay);
                OnServiceCallReceived(this, new ServiceCallerEventArgs<T>(e));

            }
        }

    }

    
}
